#include "app_time_rec.h"

#define USER_FLASH_TIME_REC_ADDR        (0x0800F400)
#define USER_FLASH_TIME_REC_SUM_SIZE    (1 * 1024)

#define USER_FLASH_TIME_REC_BUFF_SIZE           (128)           //1 Page=128 Byte    
#define USER_FLASH_TIME_REC_PAGE_CRC_DATA_SIZE  (124)           

#define GROUP_COUNT                     USER_FLASH_TIME_REC_SUM_SIZE / USER_FLASH_TIME_REC_BUFF_SIZE

app_time_rec_t sg_time_rec[GROUP_COUNT];
app_time_rec_mgr_t sg_time_rec_mgr;

void app_time_rec_init(void)
{
    sg_time_rec_mgr.sum_count = app_time_rec_sumcount();
}

unsigned char app_time_rec_sumcount(void) //返回flash 喂食数据总个数
{
    unsigned int crc;
    for (unsigned char i = 0; i < GROUP_COUNT; i++)    //1次读128byte 1k读8次 2k读16次 
    {
        drv_flash_read(USER_FLASH_TIME_REC_ADDR + i * USER_FLASH_TIME_REC_BUFF_SIZE, (unsigned char *)&sg_time_rec[i], USER_FLASH_TIME_REC_BUFF_SIZE);

        if (sg_time_rec[i].counts == 0)  
        {
            return 0;
        }
        else if (sg_time_rec[i].counts > 0 && sg_time_rec[i].counts <= 19)
        {
            crc = app_sys_crc32_cal((unsigned int *)&sg_time_rec[i].counts, USER_FLASH_TIME_REC_PAGE_CRC_DATA_SIZE);
            if (sg_time_rec[i].crc == crc)
            {
                return i * 20 + sg_time_rec[i].counts;
            }
            else return i * 20 + 0;
        }
        else if (sg_time_rec[i].counts == 20) //下一页128byte 
        {
            crc = app_sys_crc32_cal((unsigned int *)&sg_time_rec[i].counts, USER_FLASH_TIME_REC_PAGE_CRC_DATA_SIZE);
            if (sg_time_rec[i].crc == crc)
            {
                continue;
            }
            else return i * 20 + 0;
        }
        else return 0;
    }
    return 0;
}

void app_time_rec_write(unsigned char result)
{
    rtc_date_time_t real_time;
    
    unsigned char page_seek = 0;    //页定位
    unsigned char page_remainder = 0;   //当前页的喂食数据偏移
    if (sg_time_rec_mgr.sum_count == 120 * GROUP_COUNT)   return;
    // if (sg_time_rec_mgr.sum_count == 120 * GROUP_COUNT)   //存满
    // {
            
    // }
    // else
    // {
            
    // }
    page_seek = sg_time_rec_mgr.sum_count / 20;
    page_remainder = sg_time_rec_mgr.sum_count % 20;
    
    drv_rtc_GetTime(&real_time);

    sg_time_rec[page_seek].DATA[page_remainder].year = real_time.date.year;
    sg_time_rec[page_seek].DATA[page_remainder].month = real_time.date.month;
    sg_time_rec[page_seek].DATA[page_remainder].day = real_time.date.day;
    sg_time_rec[page_seek].DATA[page_remainder].hour = real_time.time.hour;
    sg_time_rec[page_seek].DATA[page_remainder].mins = real_time.time.mins;
    sg_time_rec[page_seek].DATA[page_remainder].result = result;   

    sg_time_rec[page_seek].counts = page_remainder + 1;
    sg_time_rec[page_seek].crc = app_sys_crc32_cal((unsigned int *)&sg_time_rec[page_seek].counts, USER_FLASH_TIME_REC_PAGE_CRC_DATA_SIZE);

    drv_flash_write(USER_FLASH_TIME_REC_ADDR + page_seek * USER_FLASH_TIME_REC_BUFF_SIZE, (unsigned char *)&sg_time_rec[page_seek], USER_FLASH_TIME_REC_BUFF_SIZE);

    sg_time_rec_mgr.sum_count += 1;
}


void app_clear_time_rec(void)
{
    unsigned int crc;
    sg_time_rec[0].counts = 0;  //第一页的喂食数据为0
    sg_time_rec_mgr.sum_count = 0;
    crc = app_sys_crc32_cal((unsigned int *)&sg_time_rec[0].counts, USER_FLASH_TIME_REC_PAGE_CRC_DATA_SIZE);
    sg_time_rec[0].crc = crc;

    drv_flash_write(USER_FLASH_TIME_REC_ADDR, (unsigned char *)&sg_time_rec[0], USER_FLASH_TIME_REC_BUFF_SIZE);
}
